package cn.hwua.web;

import cn.hwua.dao.AdminDao;
import cn.hwua.dao.NewsDao;
import cn.hwua.dao.impl.AdminDaoImpl;
import cn.hwua.dao.impl.NewsDaoImpl;
import cn.hwua.entity.Admin;
import cn.hwua.entity.JSONBean;
import cn.hwua.entity.News;
import cn.hwua.util.MD5Util;
import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.bind.v2.model.core.ID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@WebServlet("/manage/*")
public class AdminServlet extends HttpServlet {
    private NewsDao newsDao = new NewsDaoImpl();
    private AdminDao adminDao = new AdminDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String baseURL = req.getContextPath();//获取项目的基本路径
        String uri = req.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        if ("index.do".equals(path)) {
            req.getRequestDispatcher(baseURL + "/admin/index.jsp").forward(req, resp);
            return;
        } else if ("news_add.do".equals(path)) {
            req.getRequestDispatcher(baseURL + "/admin/news_add.jsp").forward(req, resp);
            return;
        } else if ("news_update.do".equals(path)) {
            req.getRequestDispatcher(baseURL + "/news/admin_findid.do").forward(req, resp);
            return;
        } else if ("login.do".equals(path)) {
            login(req, resp);
            return;
        } else if ("check.do".equals(path)) {
            String key = req.getParameter("key");
            String val = req.getParameter("name");
            boolean is = adminDao.check(key, val);
            JSONBean jsonBean = new JSONBean();
            if (is) {
                jsonBean.setFail("已经存在");
            } else {
                jsonBean.setSuccess("可以使用");
            }
            resp.setContentType("application/json;charset=utf-8");
            String json = JSON.toJSONString(jsonBean);
            PrintWriter out = resp.getWriter();
            out.write(json);
            out.close();
            return;
        } else if ("register.do".equals(path)) {
            String name = req.getParameter("name");
            String enpassword = req.getParameter("enpassword");
            String repassword = req.getParameter("repassword");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");
            String nickname = req.getParameter("nickname");
            JSONBean jsonBean = new JSONBean();
            enpassword = MD5Util.MD5(enpassword);
            repassword = MD5Util.MD5(repassword);
            Admin admin=new Admin();
            String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
            if (!Pattern.matches(REGEX_MOBILE,phone)){
                jsonBean.setFail("手机号码错误");
            }else {
                if (!enpassword.equals(repassword)) {
                    jsonBean.setFail("密码确认错误");
                } else {
                    admin.setAdmin_username(name);
                    admin.setAdmin_pwd(enpassword);
                    admin.setAdmin_email(email);
                    admin.setAdmin_phone(phone);
                    admin.setAdmin_nickname(nickname);
                    int save = adminDao.save(admin);
                    jsonBean.setSuccess("登录成功");
                }
            }


            resp.setContentType("application/json;charset=utf-8");
            String json = JSON.toJSONString(jsonBean);
            PrintWriter out = resp.getWriter();
            out.write(json);
            out.close();
            return;
            } else {
            throw new RuntimeException("请求错误");
        }
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        //1.验证验证码
        HttpSession session = req.getSession();
        String sessionCode = session.getAttribute("piccode").toString();
        JSONBean jsonBean = new JSONBean();
        if (!sessionCode.equalsIgnoreCase(code)) {
            jsonBean.setFail("验证码错误");
        } else {
            Admin admin = adminDao.login(name);
            if (null == admin) {
                jsonBean.setFail("用户名错误");
            } else {
                password = MD5Util.MD5(password);
                if (!password.equals(admin.getAdmin_pwd())) {
                    jsonBean.setFail("密码错误");
                } else {
                    jsonBean.setSuccess("登录成功");
                }
            }
        }
        resp.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSONString(jsonBean);
        PrintWriter out = resp.getWriter();
        out.write(json);
        out.close();
        return;
    }
}
